<?php
declare (strict_types=1);

namespace app\common\dao\activity\lottery;

use app\common\model\activity\lottery\LuckLotteryRecord;
use app\common\dao\BaseDao;


/**
 *
 * Class LuckLotteryRecordDao
 * @package app\common\dao\activity\lottery
 */
class LuckLotteryRecordDao extends BaseDao
{
    /**
     * @return string
     * @author: fupeng
     * @day: 2024/1/28
     */
    protected function getModel(): string
    {
        return LuckLotteryRecord::class;
    }

    /**
     * 获取列表
     * @param array $where
     * @param string $field
     * @param array $with
     * @param int $page
     * @param int $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList(array $where, $field = '*', array $with = [], int $page = 0, int $limit = 10)
    {
        return $this->getSearch($where)->when($with, function ($query) use ($with) {
            $query->with($with);
        })->field($field)->when($page && $limit, function ($query) use ($page, $limit) {
            $query->page($page, $limit);
        })->order('add_time desc');
    }

    public function search(array $where)
    {
        $query = LuckLotteryRecord::getDB();
        if (isset($where['lottery_id']) && $where['lottery_id'] !== '') $query->where('lottery_id', $where['lottery_id']);
        if (isset($where['is_receive']) && $where['is_receive'] !== '') $query->where('is_receive', $where['is_receive']);
        if (isset($where['is_deliver']) && $where['is_deliver'] !== '') $query->where('is_deliver', $where['is_deliver']);
        if (isset($where['type']) && $where['type'] !== '') $query->where('type', $where['type']);
        $query->when(isset($where['data']) && $where['data'] !== '', function ($query) use ($where) {
            getModelTime($query, $where['data'], 'add_time');
        });
        return $query->order('add_time desc');
    }

    /**
     * @param array $where
     * @param string $group
     * @return int
     */
    public function getCount(array $where, string $group = '')
    {
        $query = LuckLotteryRecord::getDB();
        return $query->where($where)->when($group, function ($query) use ($group) {
            $query->group($group);
        })->count();
    }
}
